	var	dmcount ;
	var	displayLang ;
	var	entityType ;
	var	entityID ;
	var	genre ;
	var	vid ;
	var	isInner ;
	var	groupID ;
	var	editClassID;
	
	var dmNameMaxLength = 50;
$(document).ready(function(){
	dmcount = parseInt($("#count").val()) ;
	displayLang = $("#display-lang").val();//must be the name "displayLang" (for fetch values)
	entityType = parseInt($("#etype").val());
	entityID = parseInt($("#eid").val());
	genre = $("#genre").val();
	vid = $("#vid").val();
	isInner = $("#isInner").val();
	groupID = $("#gid").val();
	editClassID = $("#cid").val();
	/*disable language setting during the view editing*/
	$("#set-option").unbind("click");
	
	$("body").keydown(function(event){
		var browser = detect();
		/*fix ie bug*/
		if(browser=="IE"&&(event.keyCode=="13"))
			return false;
	});
	
/*filter data items*/
	$("span.combo-arrow").mouseover(function(){
		$(this).addClass("combo-arrow-hover");
	});
	
	$("span.combo-arrow").mouseout(function(){
		$(this).removeClass("combo-arrow-hover");
	});
	
	$("span.combo-arrow").click(function(){
		$(".filter-box").show();
	});
	
	$(".filter-box .item").mouseover(function(){
		$(this).addClass("bh") ;
	});
	
	$(".filter-box .item").mouseout(function(){
		$(this).removeClass("bh") ;
	});	
	
	//show the exist data item in the left and right panel
	init();
	
    $("a.dm_create").live("click",function(){
		var location = WEBAPP+"views/dmedit.jsp?genre="+genre+"&eid="+entityID+"&i="+isInner+"&cid="+editClassID+"&dmid=-1&vtype=0"+"&lang="+displayLang;
		if(vid!="0")
			location =location+"&vid="+vid;
		window.location= location;
    });
    
    $("a.editbtn").live("click",function(){
    	var name= $(this).parent().find(".title").html();
    	name = encodeURIComponent(encodeURIComponent(name));
    	var dmid = $(this).parent().parent().attr("dmid");
    	var vtype = $(this).parent().parent().attr("vtype");
		var location = WEBAPP+"views/dmedit.jsp?genre="+genre+"&eid="+entityID+"&i="
		                  +isInner+"&vtype="+vtype+"&cid="+editClassID+"&dmid="+dmid+"&name="+name+"&lang="+displayLang;
		if(vid!="0")
			location =location+"&vid="+vid;
		window.location= location;
		event.stopPropagation();
    });
	
	/*make the data items in the editting view is sortable*/
	$('.vcontent').sortable({
		  accept:'wrapper',
		  tolerance:'pointer',
		  helper:"clone"
      });

	/*setting operation*/
	$( ".vset" ).mouseover(function(){
        $('#smenu').show();
	});

	$(".layoutOp").find(".cbutton").click(function(){
		$(this).parent().parent().parent().hide();
	});
	
	$(".top .edit-save").find(".cbutton").click(function(){
		var location = WEBAPP+"entity/"+genre+entityID;
		if(displayLang=="zh")
			location = location+"&lang=zh";
		window.location = location;
	});
	
	$(".top .edit-save").find(".sbutton").click(function(){
		var value = $("input[name=queryName]").val();
		if(value.trim()==""){
				 if(displayLang=="en")
					 $.messager.alert('Error','The name can not be empty!','error');
				 else if(displayLang=="zh")
					   $.messager.alert('错误','名字不能为空!','error');
				 return;
		}
		
		var existFlag = false;
		$(".vcontent .wrapper").each(function(index){
			if(!$(this).hasClass("default")){
				 if($(this).find(".titlebar .title>input").length>0){
					 if(displayLang=="en")
						 $.messager.alert('Error','Please confirm display names!','error');
					 else if(displayLang=="zh")
						   $.messager.alert('错误','请对显示名称进行确认!','error');
					 existFlag = true;
					 return;
				 }
			}
		});
		if(existFlag)
			return;
		
		var array = new Array();
		$(".vcontent .wrapper").each(function(index){
			if(!$(this).hasClass("default")){
				var dmtype = $(this).attr("dmtype");
				var dmid = $(this).attr("dmid");
				var inverse = $(this).attr("inverse");
				var value_type = $(this).attr("vtype");
				var func = 0;
				if($(this).hasClass("func")&&dmtype==0){
					func = 1;
				}
				
				var jsob = {};
				jsob["dmtype"] = dmtype;
				jsob["dmid"] = dmid;
				jsob["inverse"] = inverse;
				jsob["vtype"] = value_type;
				jsob["func"] = func;
				var label = $(this).find(".titlebar .title").html().trim();
				jsob["label"] = label;
				array.push(jsob) ;
			}
		});
		var vlayout = $("#vlayout").val();
		
		var result =storeViewInfo(vid,value,groupID,isInner,editClassID,displayLang,array,vlayout);
		
		var status = result["status"];
		if(status=="ok"){
			var location = WEBAPP+"entity/"+genre+entityID;
			if(displayLang=="zh")
				location = location+"&lang=zh";
			window.location =location;
		}else if(status=="conflict"){
			 if(displayLang=="en")
				 $.messager.alert('Error','The view name has existed!','error');
			 else if(displayLang=="zh")
				   $.messager.alert('Error','视图名称已经存在!','error');
		}else if(status=="noauthority"){
			if(displayLang=="en")
				 $.messager.alert('Info','The view is being edited elsewhere. Please retry later!','info');
			 else if(displayLang=="zh")
				 $.messager.alert('提示','视图在别处正被编辑。请稍后再试!','info');
		}else if(status=="removed"){
			if(displayLang=="en")
				 $.messager.alert('Info','The view has been deleted elsewhere. Your update can not be saved !','info');
			 else if(displayLang=="zh")
				 $.messager.alert('提示','该视图已在别处删除。您的更新无法保存!','info');
		}
		
	});
	
	$('#layoutSet').click(function(){
		$(".viewSetOp").hide();
		$(".layoutOp").show();
		$('#smenu').hide();
	});
	
	//save layout 
	$(".layoutOp").find(".sbutton").click(function(){
		$(this).parent().parent().parent().hide();
		var value = $("input[name=layout]:checked").val();
		if(value==1){
			$(".vcontent .wrapper").width("670px");
		}else if(value==2){
			$(".vcontent .wrapper").width("318px");
		}else if(value==3){
			$(".vcontent .wrapper").width("200px");
		}
		$("#vlayout").val(value);
	});

	$("a.delete").live("click",function(event){
	    event.stopPropagation();
		$(this).parent().parent().parent().remove();
	});
	
	$("a.close").live("click",function(event){
	   if($(this).parent().parent().parent().hasClass("wrapper")){
			var ancest = $(this).parent().parent().parent();
			var dmtype = ancest.attr("dmtype");
			var dmid = ancest.attr("dmid");
			var button = $(".dmList .li-container[dmtype="+dmtype+"][dmid="+dmid+"]").find("button.addbtn");
			enableAddBtn(button);
		}
		$(this).parent().parent().parent().remove();
		event.stopPropagation();
	});
	
	
	
	   $(".pvalue li,.lvalue li,.hitem").live("mouseover",function(){
		   $(this).addClass("bh");
	   });
	   
	   $(".pvalue li,.lvalue li,.hitem").live("mouseout",function(){
		   $(this).removeClass("bh");
	   });
	   
	   $("#dms .titlebar").live("click",function(){
		   $(this).find("a.status").toggleClass("collapse");
		   $(this).parent().find(".content").toggle('fast');
	   });
	   $(".vcontent .titlebar").live("click",function(){
		   $(this).find("a.status").toggleClass("collapse");
		   $(this).parent().find(".content").toggle('fast');
	   });
	   
	  /*rename*/
		$(".vcontent span.title").live("mouseover",function(){
			$(this).addClass("hover");
		});
		
		$(".vcontent span.title").live("mouseout",function(){
			$(this).removeClass("hover");
		});
		
		$(".vcontent span.title").live("click",function(){
			rename(this);
			event.stopPropagation();
		});
		
		/*add datamember to view*/
		$(".addbtn").live("click",function(event){
			if($(this).hasClass("disabled"))
				return false;
			var addElement = $(this).parent().parent().parent();
			var dmtype = addElement.attr('dmtype');
			var dmid = addElement.attr('dmid');
			if($(".leftPanel").find(".wrapper[dmtype="+dmtype+"][dmid="+dmid+"]").length>0){
				return;
			}
			addWrapper($(".vcontent"));
			var defaultWrapper= $(".default");
			defaultWrapper.removeClass("default");
            var flag =  dropFunction(defaultWrapper,addElement.parent());
            event.stopPropagation();
            disableAddBtn($(this));
		});
	}
);

function addDMToLeftPanel(dmtype,dmid,arity,inverse,vtype,funct,hasAuthor,name,flag,isMygroup){
	$(".dmList").html("");
//	$(".dmList").find(".li-container[dmtype="+dmtype+"][dmid="+dmid+"]").parent().remove();
	var li = $("<li class='prop'></li>");
	$(".dmList").prepend(li);
	var div  = $("<div class='li-container origin'></div>");
	li.append(div);
	div.attr("inverse",inverse);
	div.attr("vtype",vtype) ;
	
	var mid = getMID();
	div.attr("mid",mid);
	div.attr("dmtype",dmtype);
	div.attr("dmid",dmid) ;
	div.attr("arity",arity);
	div.attr("editable",hasAuthor);
	
	if(funct==1){
		li.addClass("func");
		if(vtype==1){
			li.addClass("func_literal");
		}
	}
	
    var titleDiv = $("<div class='titlebar'></div>");
    div.append(titleDiv);
    var spanAdd = $("<span class='adding'></span>");
    titleDiv.append(spanAdd);
    var spanLoading = $("<span class='loading'></span>");
    titleDiv.append(spanLoading);
    addExpandButtonToBar(titleDiv);
    var span = $("<span class='title'></span>");
    name  = cutText(name,dmNameMaxLength);
    span.text(name);
    titleDiv.append(span);
    if(isMygroup){
    	addDMEditButtonToBar(titleDiv);
    }
    
    var content = $("<div class='content'></div>");
	var pvalue = $("<ul class='pvalue'></ul>");
    div.append(content) ;
	content.append(pvalue);
	
	var opBox = $("<div class='dm_op_box'></div>");
	div.append(opBox);
	addAddButtonToBar(opBox);
    if($(".leftPanel .wrapper[dmtype="+dmtype+"][dmid="+dmid+"]").length>0){
   		disableAddBtn(opBox.find("button"));
    }
	
    var status = "undownloaded" ;
    if(flag)
    	fetchValues(displayLang,pvalue,dmtype,dmid,inverse,genre,entityID,status,false);
	return li;
}

function addExpandButtonToBar(bar){
	var button = $('<a class="status"></a>');
	bar.append(button);
}

function addAddButtonToBar(bar){
	var button = $('<button class="addbtn btn btn-primary btn-small"></button>');
	if(displayLang=="en"){
		button.html("Add");
    }else{
    	button.html("添加");
    }
	bar.append(button);
}

function addDMEditButtonToBar(bar){
	var button = $('<a class="easyui-linkbutton editbtn" plain="true" iconCls="icon_editbtn" '+
		'href="javascript:void(0)"></a>');
	if(displayLang=="en"){
	button.attr("title","Edit this query item");
    }else{
	button.attr("title","编辑这个查询项");
    }
	button.linkbutton();
	bar.append(button);
}

function addRemoveButtonToBar(bar){
	var button = $('<a class="close"></a>');
 	var op_title= null;
	if(displayLang=="en")
		op_title = "Remove";
	else if(displayLang=="zh")
		op_title = "移除"; 	
	button.attr("title",op_title);
	$(bar).append(button);	
}

function addTitleToBar(title,bar){
	bar.attr("title",title);
}

function addWrapper(container){
	 var div=null;
	 if(displayLang=="en"){
		 div = $("<div class='wrapper default'><div class='defaultBox'></div></div>");
	 }else if(displayLang=="zh"){
		 div = $("<div class='wrapper default'><div class='defaultBox'></div></div>");
	 }
	 
	 container.append(div);
	}

//from:content: <div class='content'>, container: content's container 
function copyContent(content,container){
	container.append(content);
	container.find(".fsetBox").remove();
}

//wrapper:the wrapper container
//left:belong to li-container ,it shouled be cloned
function copyFromLeftToRight(left,wrapper){
	$(wrapper).html("");
	//set mid and origin
  if(left.hasClass("origin")){
		  $(wrapper).addClass("origin");
	}
  if($(left).parent().hasClass("func")){
  	$(wrapper).addClass("func");
  }
  if($(left).parent().hasClass("func_literal")){
  	$(wrapper).addClass("func_literal");
  }
  
	$(wrapper).attr("mid",left.attr("mid"));
	$(wrapper).attr("inverse",left.attr("inverse"));
	$(wrapper).attr("vtype",left.attr("vtype"));
	var _dmtype =left.attr("dmtype");
	$(wrapper).attr("dmtype",_dmtype);
	var _dmid = left.attr("dmid");
	$(wrapper).attr("dmid",_dmid);
	$(wrapper).attr("arity",left.attr("arity"));
	$(wrapper).attr("dmid",left.attr("dmid"));
	var container = $("<div class='li-container'></div>");
	var titlebar = createTitleBar();
    var titleSpan = $("<span class='title'></span>");
	titleSpan.text(left.find(".titlebar .title").text());
	addExpandButtonToBar(titlebar);
	titlebar.append(titleSpan) ;
	addRemoveButtonToBar(titlebar);
	container.append(titlebar);
	copyContent(left.parent().find(".content").clone().show(),container) ;
	 $(wrapper).append(container) ;
}

//create empty title bar without title , buttons
function createTitleBar(){
	 var titlebar = $("<div class='titlebar'></div>");
	 return titlebar;
}

function disableAddBtn(button){
    button.addClass("disabled");
}

function disableSortable(){
	$(".vcontent").sortable({ disabled: true });
}

function enableAddBtn(button){
	 button.removeClass("disabled");
}

function enableSortable(){
	$(".vcontent").sortable({ disabled: false });
}

/*add left panel to right panel*/
function dropFunction(dropEle,dragEle){
	 var flag = false;
      //remove all the highlighted wrapper,not only this
	  //copy everything contains mid origin status
      var dragContainer = $(dragEle).find(".li-container");
      //see if merge
      if($(dropEle).find(".li-container").length>0){
    	  return flag;
      }else{
    	  copyFromLeftToRight(dragContainer,dropEle);
	      flag = true;
     }
     return flag;
}

function fetchValueByContainer(DivContainer){
	  var dmtype  = parseInt(DivContainer.attr("dmtype")) ;
      var dmid =  parseInt(DivContainer.attr("dmid")) ;
      var inverse =  parseInt(DivContainer.attr("inverse")) ;
      var status = "undownloaded" ;
      fetchValues(displayLang,DivContainer.find(".pvalue"),dmtype,dmid,inverse,genre,entityID,status);
}

function filterDMByBackward(lang,etype,eid){
//	$(".dmcreate-box").hide();
	var label;
	if(displayLang=="en"){
		label = "Backward";
	}else if(displayLang=="zh"){
		label="后向向属性";
	}
	$(".searchText").val(label);
	$("#dms .dmList").html("");
	 var data={};
	 data["lang"] = lang;
	 data["eid"] = eid;
	 data["etype"] = etype;
	 data =  JSON.stringify(data);
	 data = ("backward="+encodeURIComponent(data));
	 $.ajax({
	  	  url:VESERV,
	  	  async:true,
	  	  data:data,
	  	  dataType:"json",
	  	  type:"post",
	      success:function(result){
	          var containers = new Array();
//	    	  for(i=0;i<result.length;i++){
	    	  for(i=result.length-1;i>=0;i--){
	    		var item = result[i];
			    var dmtype = item.dmtype;
			    var dmid = item.dmid;
			    var inverse = item.inverse;
			    var name = item.name;
			    var vtype = item.vtype;
			    var arity = item.arity;
			    var funct = item.functional;
			    var hasAuthor =item.hasAuthor; 
			    var li = addDMToLeftPanel(dmtype,dmid,arity,inverse,vtype,funct,hasAuthor,name,0) ;
			    containers.push(li) ;
	    	  }
	    	  for(i=0;i<containers.length;i++){
	    		  fetchValueByContainer(containers[i].find(".li-container"),eid);
	    	  }
	      }
	    });
}

function filterDMByForward(lang,etype,eid){
//	$(".dmcreate-box").hide();
	var label;
	if(displayLang=="en"){
		label = "Forward";
	}else if(displayLang=="zh"){
		label="前向属性";
	}
	$(".searchText").val(label);
	$("#dms .dmList").html("");
	 var data={};
	 data["lang"] = lang;
	 data["eid"] = eid;
	 data["etype"] = etype;
	 data =  JSON.stringify(data);
	 data = ("forward="+encodeURIComponent(data));
	 $.ajax({
  	  url:VESERV,
  	  async:true,
  	  data:data,
  	  dataType:"json",
  	  type:"post",
      success:function(result){
          var containers = new Array();
//    	  for(i=0;i<result.length;i++){
    	  for(i=result.length-1;i>=0;i--){
    		var item = result[i];
		    var dmtype = item.dmtype;
		    var dmid = item.dmid;
		    var inverse = item.inverse;
		    var name = item.name;
		    var vtype = item.vtype;
		    var arity = item.arity;
		    var funct = item.functional;
		    var hasAuthor =item.hasAuthor; 
		    var li = addDMToLeftPanel(dmtype,dmid,arity,inverse,vtype,funct,hasAuthor,name,0) ;
		    containers.push(li) ;
    	  }
    	  for(i=0;i<containers.length;i++){
    		  fetchValueByContainer(containers[i].find(".li-container"),eid);
    	  }
      }
    });
}

function filterDMByMyGroup(groupID,lang,etype,eid,isInner,cid){
	var label;
	if(displayLang=="en"){
		label = "My group";
	}else if(displayLang=="zh"){
		label="我的组";
	}
	$(".searchText").val(label);
//	$(".dmcreate-box").show();
	$("#dms .dmList").html("");
	 var data={};
	 data["gid"]=groupID;
	 data["lang"] = lang;
	 data["eid"] = eid;
	 data["etype"] = etype;
	 data["isInner"]=isInner;
	 data["cid"]=cid;
	 data =  JSON.stringify(data);
	 data = ("mygroup="+encodeURIComponent(data));
	 $.ajax({
 	  url:VESERV,
 	  async:true,
 	  data:data,
 	  dataType:"json",
 	  type:"post",
     success:function(result){
         var containers = new Array();
//   	  for(i=0;i<result.length;i++){
   	  for(i=result.length-1;i>=0;i--){
   		    var item = result[i];
		    var dmtype = item.dmtype;
		    var dmid = item.dmid;
		    var inverse = item.inverse;
		    var name = item.name;
		    var vtype = item.vtype;
		    var arity = item.arity;
		    var funct = item.functional;
		    var hasAuthor =item.hasAuthor; 
		    var li = addDMToLeftPanel(dmtype,dmid,arity,inverse,vtype,funct,hasAuthor,name,0,1) ;
		    containers.push(li) ;
   	     }
   	     for(i=0;i<containers.length;i++){
   		  fetchValueByContainer(containers[i].find(".li-container"),eid);
   	     }
     }
   });
}


function filterDMByOtherGroup(groupID,lang,etype,eid,isInner,cid){
//	$(".dmcreate-box").hide();
	var label;
	if(displayLang=="en"){
		label = "Other groups";
	}else if(displayLang=="zh"){
		label="其它组";
	}
	$(".searchText").val(label);
	$("#dms .dmList").html("");
	 var data={};
	 data["gid"]=groupID;
	 data["lang"] = lang;
	 data["eid"] = eid;
	 data["etype"] = etype;
	 data["isInner"]=isInner;
	 data["cid"]=cid;
	 data =  JSON.stringify(data);
	 data = ("othergroups="+encodeURIComponent(data));
	 $.ajax({
 	  url:VESERV,
 	  async:true,
 	  data:data,
 	  dataType:"json",
 	  type:"post",
     success:function(result){
        var containers = new Array();
//  	  for(i=0;i<result.length;i++){
  	  for(i=result.length-1;i>=0;i--){
   		var item = result[i];
		    var dmtype = item.dmtype;
		    var dmid = item.dmid;
		    var inverse = item.inverse;
		    var name = item.name;
		    var vtype = item.vtype;
		    var arity = item.arity;
		    var funct = item.functional;
		    var hasAuthor =item.hasAuthor; 
		    var li = addDMToLeftPanel(dmtype,dmid,arity,inverse,vtype,funct,hasAuthor,name,0) ;
		    containers.push(li) ;
   	  }
   	  for(i=0;i<containers.length;i++){
   		  fetchValueByContainer(containers[i].find(".li-container"),eid);
   	  }
     }
   });
}

function getMID(){
    dmcount = dmcount+1;
    return dmcount-1;
}

//show the exist datammeber in the left and right panel
//show the exist view name in the text input
function init(){
	if(vid>0){
		//show the exist view name in the text input
		initViewDM(vid);
	}
	/*search data items*/
	/*input hints*/
	//quick search widget
  	$.widget( "custom.mycomplete", $.ui.autocomplete, {
		_renderMenu: function( ul, items ) {
			var that = this;
			currentCategory = "";
			$.each( items, function( index, item ) {
				if ( item.category != currentCategory ) {
					var ctext = item.category;
					if(ctext=="mygroup"){
						ctext = "my group";
					}else if(ctext=="othergroups"){
						ctext = "other groups";
					}
					var li = $("<li class='ui-autocomplete-category'></li>");
					li.text(ctext);
					li.attr("category",item.category);
					ul.append(li);
					currentCategory = item.category;
				}
				that._renderItemData( ul, item );
			});
		},
		_renderItemData:function( ul, item ) {
		    var dmtype = item.dmtype;
		    var dmid = item.dmid;
		    var inverse = item.inverse;
		    var name = item.name;
		    var vtype = item.vtype;
		    var arity = item.arity;
		    var funct = item.functional;
		    var hasAuthor =item.hasAuthor;
		    var category = item.category;
		    
		    var isMyGroup = false;
		    if(category=="mygroup")
		    	isMyGroup = true;
		    var li = showDMSearchItem(dmtype,dmid,arity,inverse,vtype,funct,name,hasAuthor,isMyGroup);//showMycompleteItem(vid,name,category);
			li.appendTo(ul);
			ul.find("li:last>div").addClass("final");
			return li;
		}
	});
	$(".qitem-box-inview .dmsearch" ).mycomplete({
		source: function( request, response ) {
			 var data={};
			 data["kw"] = request.term;
			 data["lang"] = displayLang;
			 data["etype"] = parseInt(entityType);
			 data["eid"] = parseInt(entityID);
			 var isInner = $("#isInner").val();//minTypeArray[0] ;
			 var classID = editClassID ;//minTypeArray[1] ;
			 data["isInner"] = isInner;
			 data["cid"] = classID ;
			 data["user"] = getUser();
			 data["groupID"] = groupID;
		     data =  JSON.stringify(data);
			 data=("dmsearch="+encodeURIComponent(data));
			$.ajax({
				url: VESERV,//views_servlet,//
				dataType: "json",
				data: data,
				type:"post" ,
				success: function( data ) {
					response(data);
				}
			});
		},
		minLength:0
	}).focus(function () {
		if ($(this).mycomplete("widget").is(":visible")) {
             return;
        }
		$(this).mycomplete("search", $(this).val());
    });
}

function initDMToRightPanel(wrapper,dmtype,dmid,arity,inverse,vtype,funct,name,flag){
	$(wrapper).html("");
	//set mid and origin
   $(wrapper).addClass("origin");
   if(funct==1){
	  $(wrapper).addClass("func");
		if(vtype==1){
			$(wrapper).addClass("func_literal");
		}
	}
  
    var mid = getMID();
    $(wrapper).attr("mid",mid);
	$(wrapper).attr("inverse",inverse);
	$(wrapper).attr("vtype",vtype);
	$(wrapper).attr("dmtype",dmtype);
	$(wrapper).attr("arity",arity);
	$(wrapper).attr("dmid",dmid);
	
	var container = $("<div class='li-container'></div>");
	var titlebar = createTitleBar();
	var spanLoading = $("<span class='loading'></span>");
	titlebar.append(spanLoading);
	var titleSpan = $("<span class='title'></span>");
	name = cutText(name,dmNameMaxLength);
	titleSpan.text(name);
	addExpandButtonToBar(titlebar);
	titlebar.append(titleSpan) ;
	addRemoveButtonToBar(titlebar);
	container.append(titlebar);
	
    var content = $("<div class='content'></div>");
	var pvalue = $("<ul class='pvalue'></ul>");
	container.append(content) ;
	content.append(pvalue);
	
	$(wrapper).append(container) ;
	 
}

/*get data item of view dm*/
function initViewDM(vid){
	 var data={};
	 data["vid"] = vid;
	 data =  JSON.stringify(data);
	 data = ("viewDMInit="+encodeURIComponent(data));
	 
	 $.ajax({
		   url:VESERV,
	   	   async:false,
	   	   data:data,
	   	   dataType:"json",
	   	   type:"post",
		   success: function( data ) {
			    var mArray = new Array();
			    for(i=0;i<data.length;i++){
					addWrapper($(".vcontent"));
					var item = data[i];
					var dmtype = item.dmtype;
					var dmid = item.dmid;
					var inverse = item.inverse;
					var name = item.name;
					var vtype = item.vtype;
					var arity = item.arity;
					var funct = item.functional;
					
					var defaultWrapper= $(".default");
					defaultWrapper.removeClass("default");
					initDMToRightPanel(defaultWrapper,dmtype,dmid,arity,inverse,vtype,funct,name);
					mArray.push(defaultWrapper) ;
					
				}
				for(i=0;i<mArray.length;i++){
					var mli = $(mArray[i]);
					var mid = mli.attr("mid");
				    var status = "undownloaded" ;
				    var pvalue = mli.find(".pvalue");
				    var dmtype = parseInt(mli.attr("dmtype"));
				    var dmid = parseInt(mli.attr("dmid"));
				    var inverse = parseInt(mli.attr("inverse"));
				    fetchValues(displayLang,pvalue,dmtype,dmid,inverse,genre,entityID,status,true);
				}
		   }
	 });
}

/*rename*/
function rename(e){
	//see if the wrapper is origin
	var wrapperAncestor =  $(e).parent().parent().parent();
	var isOrigin =wrapperAncestor.hasClass("origin");
	var element  = $(e).parent().parent().find(".title");
	var textBox = $('<input type="text" class="test"></input>');
	var value= element.text();
    textBox.attr('value',value);
  	element.html('');
	element.append(textBox);    
  	textBox.focus();
  	textBox.select();
  	disableSortable();
  	
	textBox.click(function(event){
		event.stopPropagation();
	});
	textBox.focusout(function(event){
		saveEditName(element,textBox.val(),value,isOrigin);
		enableSortable();
		event.stopPropagation();
	});
	textBox.bind("keyup",function(event){
		if (event.keyCode=="13"){
			saveEditName(element,textBox.val(),value,isOrigin);
			enableSortable();
			element.removeClass("hover");
		}
	});
}

function saveEditName(element, value,valueOld,isOrigin){
  if(value.trim()==""){
	 if(displayLang=="en")
		 $.messager.alert('Error','The name can not be empty!','error');
	 else if(displayLang=="zh")
		   $.messager.alert('Error','名字不能为空!','error');
  	element.html(valueOld);
    return;
  }
  
  element.html(value);
  var ancestor  =  	element.parent().parent().parent();
  if(isOrigin){
  	//remove origin
    ancestor.removeClass("origin");
  } 
}

/*create the html of li and bind events
 * return the li
 * */
function showDMSearchItem(dmtype,dmid,arity,inverse,vtype,funct,name,hasAuthor,isMyGroup){
	var li = $("<li class='hitem lentity'></li>");
	var div = $('<div class="split"></div>');
	
	var a = $('<a href="javascript:void(0)"></a>');
	a.attr("dmtype",dmtype) ;		
	a.attr("dmid",dmid) ;	
	div.append(a);
	a.click(function(){
		$(".qitem-box-inview .dmsearch").val(name);
		$(".qitem-box-inview .dmsearch").removeClass("focus");
		$(".qitem-box-inview .dmsearch").mycomplete("close");
		addDMToLeftPanel(dmtype,dmid,arity,inverse,vtype,funct,hasAuthor,name,1,isMyGroup);
	});
	
	var spanName = $("<span class='entity'></span>") ;
	spanName.text(name);
	a.append(spanName);
	li.append(div);
	return li;
}

function storeViewInfo(viewID,name,groupID,isInner,classID,lang,array,vlayout){
	var result; 
	
	var user = getUser();
	 var data={};
	 data["viewID"] = viewID;
	 data["name"] = name;
	 data["groupID"] = groupID;
	 data["isInner"] = isInner;
	 data["classID"] =classID;
	 data["lang"] =lang;
	 data["user"] = user;
	 data["dms"] = array;
	 data["layout"] = vlayout;
	 data =  JSON.stringify(data);
	 data = ("storeviewinfo="+encodeURIComponent(data));
	 
	 $.ajax({
	   	  url:VESERV,
	   	  async:false,
	   	  data:data,
	   	  dataType:"json",
	   	  type:"post",
	   	  beforeSend:function(){
	   		  var div = $("<div class='load-data2'></div>");
	   		  $("body").append(div);
	   	  },
	   	  success:function(json){
	   		 $("body").find(".load-data2").remove();
	   		  result = json;
	   	  }
     });
	 
	 return result;
}




